package cn.iocoder.yudao.module.gpt.dal.redis.chatsession;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.iocoder.yudao.module.gpt.api.dto.GPTUserMessageDTO;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;

@Repository
public class ChatSessionRedisDao {

    @Resource
    private RedisTemplate<String, ChatSessionRedisDO> redisTemplate;

    public List<ChatSessionRedisDO> searchHistory(String sessionId){
        Set<ChatSessionRedisDO> orderedSet = redisTemplate.opsForZSet().rangeByScore(sessionId, 0, -1);
        List<ChatSessionRedisDO> orderedList = new ArrayList<>();
        CollUtil.addAll(orderedList,orderedSet);
        return orderedList;
    }
    public void insertHistory(ChatSessionRedisDO chatSessionRedisDO,String sessionId){
        redisTemplate.opsForZSet().add(sessionId,chatSessionRedisDO, System.currentTimeMillis());
    }
}
